import axios, {AxiosError, AxiosResponse} from 'axios'
import {AxiosRequestConfig} from 'axios';
import app from "@/App.vue";
import {load} from "@/utils/loading";

const service = axios.create({
        baseURL: 'lunaApi',
        timeout: 10000,
        headers: {
            "Content-Type": "application/json;charset=utf-8"
        }
    }
);
// 添加请求拦截器
service.interceptors.request.use(
    (config) => {
        // 在发送请求之前做些什么 token
        load.show();
        if (localStorage.getItem('token')) {
            config.headers!['token'] = `${localStorage.getItem('token')}`;
        }
        return config;
    },
    (error) => {
        // 对请求错误做些什么
        return Promise.reject(error);
    }
);

service.interceptors.response.use((res: AxiosResponse) => {
    load.hide();
    console.log('res',res);
    const code = res.status;
    if (code != 200) {
        return Promise.reject();
    }
    return res.data;
}, (err: AxiosError) => {
    load.hide();
    console.log(err);
})
export default service;